package com.colin.dubbo.strategy.impl;

import com.alibaba.fastjson.JSONObject;
import com.colin.dubbo.annotation.MethodName;
import com.colin.dubbo.business.request.Demo1Request;
import com.colin.dubbo.business.response.CommonResponse;
import com.colin.dubbo.datasource.DataSource;
import com.colin.dubbo.datasource.DataSourceTypeEnum;
import com.colin.dubbo.entity.dto.RequestDto;
import com.colin.dubbo.entity.po.AccountPO;
import com.colin.dubbo.enums.MethodEnum;
import com.colin.dubbo.mapper.AccountMapper;
import com.colin.dubbo.strategy.HandleStrategy;
import com.colin.dubbo.util.LogUtil;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 示例策略一
 */
@MethodName(method = MethodEnum.DEMO1)
public class Demo1HandleStrategy implements HandleStrategy {

    @Resource
    private AccountMapper accountMapper;
    
    @Override
    @DataSource(dataSourceType = DataSourceTypeEnum.MYSQL_MASTER)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResponse handle(RequestDto request) throws Exception {
        CommonResponse response = CommonResponse.success();
        Demo1Request demo1Request = (Demo1Request) request;
        
        LogUtil.info("demo1:{}", JSONObject.toJSONString(demo1Request));

        AccountPO account = new AccountPO();
        account.setAmount(0.01D);
        account.setMonth("05");
        account.setCreateTime(new Date());
        account.setYear("2021");
        account.setUserId(1L);
        account.setComments("测试");
        int i = accountMapper.insertAndReturnId(account);
        LogUtil.info("insert_success:{}", account.getAccountId());

        response.setData(request);
        return response;
    }
}
